{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "br = webdriver.Chrome()\n",
    "br.get('https://www.tmall.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "input_element = br.find_element_by_xpath('//*[@id=\"mq\"]')\n",
    "input_element.clear()\n",
    "input_element.send_keys(\"MacBook pro\")\n",
    "search_element = br.find_element_by_xpath('//*[@id=\"mallSearch\"]/form/fieldset/div/button')\n",
    "search_element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "html_str = br.page_source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "products = html.xpath('.//div[@id=\"J_ItemList\"]/div')\n",
    "print(len(products))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Apple/苹果 13 英寸 2.3GHz 处理器 256GB 存储容量 MacBook Pro'] ['11786.00'] ['\\nApple Store 官方旗舰店\\n']\n",
      "['Apple/苹果 触控栏和触控 ID 2.3GHz 处理器 256GB 存储容量 13 英寸 MacBook Pro'] ['12588.00'] ['\\n卓辰数码旗舰店\\n']\n",
      "['Apple/苹果 触控栏和触控 ID 2.6GHz 处理器 512GB 存储容量 15 英寸 MacBook Pro'] ['19588.00'] ['\\n卓辰数码旗舰店\\n']\n",
      "['Apple/苹果 13英寸 MacBook Pro'] ['10888.00'] ['\\n苏宁易购官方旗舰店\\n']\n",
      "['Apple/苹果 13 英寸 2.3GHz 处理器 128GB 存储容量 MacBook Pro'] ['10200.00'] ['\\nApple Store 官方旗舰店\\n']\n",
      "['Apple/苹果 触控栏和触控 ID 2.3GHz 处理器 512GB 存储容量 13 英寸 MacBook Pro'] ['14088.00'] ['\\n卓辰数码旗舰店\\n']\n",
      "['Apple/苹果 触控栏和触控 ID 2.2GHz 处理器 256GB 存储容量 15 英寸 MacBook Pro'] ['16968.00'] ['\\n苏宁易购官方旗舰店\\n']\n",
      "['Apple/苹果 13英寸：MacBook Pro 2.3GHz 处理器 128GB 存储容量'] ['9288.00'] ['\\n苏宁易购官方旗舰店\\n']\n",
      "['Apple/苹果 13英寸：MacBook Pro 128GB'] ['9399.00'] ['\\n绿森数码官方旗舰店\\n']\n",
      "['Apple/苹果 13英寸：MacBook Pro 256GB'] ['10838.00'] ['\\n卓辰数码旗舰店\\n']\n",
      "['Apple/苹果 MacBook Pro MJLQ2CH/A'] ['13188.00'] ['\\n禾木林数码专营店\\n']\n",
      "['Apple/苹果 15英寸：MacBook Pro 2.9GHz 处理器 512GB'] ['18388.00'] ['\\n岗隆数码专营店\\n']\n",
      "['Apple/苹果 13英寸：MacBook Pro 3.1GHz 处理器 512GB'] ['13988.00'] ['\\n顺云数码专营店\\n']\n",
      "['Apple/苹果 13英寸：MacBook Pro 3.1GHz 处理器 256GB'] ['12888.00'] ['\\n顺云数码专营店\\n']\n"
     ]
    }
   ],
   "source": [
    "for product in products:\n",
    "    title = product.xpath('./div/p[2]/a/@title')\n",
    "    price = product.xpath('./div/p[1]/em/@title')\n",
    "    shop = product.xpath('./div/div[2]/a[1]/text()')\n",
    "    print(title,price,shop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
